package com.test;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

public class FilmAccept extends Activity {
	private static final String TAG = "Film Accept";
	private Preview mPreview;
	private ServerSocket server;
	private Socket socket;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
	public void onResume(){
		super.onResume();
		Log.i(TAG, "FilmAccept onResume");
		try {
			server = new ServerSocket(8000);
			socket = server.accept();
			Log.i(TAG, "FilmAccept socket accepted");
			setVideoStreaming(socket);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void onPause(){
		super.onPause();
		try {
			Log.i(TAG, "FilmAccept onPause");
			server.close();
			Log.i(TAG, "FilmAccept socket.close()");
			socket.close();
			Log.i(TAG, "FilmAccept socket closed");
			mPreview.done();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setVideoStreaming(Socket socket){
		//creates the video SurfaceView and adds it to the FrameLayout in the center of the new ContentView
		Log.i(TAG, "FilmAccept setVideoStreaming");
		FrameLayout frame = ((FrameLayout) findViewById(R.id.preview));
		frame.removeAllViews();
		mPreview = new Preview(this, socket, frame.getWidth(), frame.getHeight());	
		  
		frame.addView(mPreview);	

		Log.d(TAG, "onCreate'd");

		
	}

	public void OkBtn_onClick(View view){
		mPreview.done();
		//this.ok();
		toast("Goodbye");
		
		finish();
	}
	public void call911Btn_onClick(View view){
		toast("We're sorry, the number you have dialed is out of service.");
	}

	private void toast(String messageText) {
		Toast toast = Toast.makeText(
				this,
				messageText,
				Toast.LENGTH_LONG
		);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	class Preview extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private SocketCamera mCamera;
		private Socket socket;
		private int width, height;

		Preview(Context context, Socket socket, int width, int height) {
			super(context);
			Log.i(TAG, "FilmAccept Preview()");
			
			//set socket
			this.socket = socket;
			this.width = width;
			this.height = height;

			// Install a SurfaceHolder.Callback so we get notified when the
			// underlying surface is created and destroyed.
			mHolder = getHolder();
			mHolder.addCallback(this);
		}

		public void done(){
			Log.i(TAG, "FilmAccept done()");
			if(mCamera!=null){
				mCamera.stopPreview(); 
				mCamera.release(); 
				mCamera = null;
			}
		}
		public void surfaceCreated(SurfaceHolder holder) {
			// The Surface has been created, acquire the camera and tell it where
			// to draw.
			//set the SocketCamera with an inputStream

			Log.i(TAG, "FilmAccept surfaceCreated");
			try {
				mCamera = new SocketCamera (socket, width, height);
				mCamera.setPreviewDisplay(holder);
			} catch (IOException exception) {
				done();
				// TODO: add more exception handling logic here
			}
		}

		public void surfaceDestroyed(SurfaceHolder holder) {
			// Surface will be destroyed when we return, so stop the preview.
			// Because the CameraDevice object is not a shared resource, it's very
			// important to release it when the activity is paused.
			Log.i(TAG, "FilmAccept surfaceDestroyed");	
			done();
		}

		public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
			// Now that the size is known, set up the camera parameters and begin
			// the preview.
			Log.i(TAG, "FilmAccept surfaceChanged");
			mCamera.startPreview();
		}

	}

}